package com.dongwu.manager.web;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dongwu.common.dto.PagingVO;
import com.dongwu.common.dto.Result;
import com.dongwu.common.utils.DateUtils;
import com.dongwu.common.web.BaseController;
import com.dongwu.common.web.Servlets;
import com.dongwu.manager.entity.DeviceProductRecord;
import com.dongwu.manager.service.DeviceAdsRecordService;
import com.dongwu.manager.service.DeviceProductRecordService;
import com.dongwu.manager.service.DeviceService;
import com.dongwu.security.entity.User;
import com.dongwu.security.service.IUserService;
@Controller
@RequestMapping("/manager/deviceproductrecord")
public class DeviceProductRecordController extends BaseController<DeviceProductRecord, String>{

	@Autowired
	private DeviceProductRecordService deviceProductRecordService;
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private DeviceService deviceService;
	
	@Autowired
	private DeviceAdsRecordService deviceAdsRecordService;
	
	@RequestMapping("/throw")
	public String waitdistlist() {
		return this.getPagePath("throw");
	}
	/**
	 * 
	 * @param remark
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping(value = "/addRecord")
	@ResponseBody
	public Result addRecord(String remark, String startTime, String endTime,
			@RequestParam(value = "ids[]") String[] ids, @RequestParam(value = "pids[]") String[] pids)
			throws ParseException {
		List<DeviceProductRecord> insdpr = new ArrayList<DeviceProductRecord>();
		List<DeviceProductRecord> upddpr = new ArrayList<DeviceProductRecord>();
		for (int i = 0; i < ids.length; i++) {
			User user = deviceService.get(ids[i]).getUser();
			for (int j = 0; j < pids.length; j++) {
				DeviceProductRecord r = new DeviceProductRecord();
				r.setUser(user);
				r.setDeviceid(ids[i]);
				if (startTime != "") {
					r.setStartTime(DateUtils.parseDate(startTime, DateUtils.PATTERN_DATE_TIME));
				}
				if (endTime != "") {
					r.setEndTime(DateUtils.parseDate(endTime, DateUtils.PATTERN_DATE_TIME));
				}
				r.setProductid(pids[j]);
				r.setStatus(0);
				r.setRemark(remark);
				// 获取当前设备的所有投放记录
				List<DeviceProductRecord> dprList = deviceProductRecordService.findByDeviceId(ids[i]);
				if (dprList.size() != 0) {
					boolean isexist = true;
					// 如果记录存在则遍历记录
					for (DeviceProductRecord dpr : dprList) {
						// 如果已投放当前商品则更新
						if (dpr.getProductid().equals(pids[j])) {
							r.setId(dpr.getId());
							upddpr.add(r);
							isexist = false;
						}
					}
					// 如果当前设备的当前商品不存在则添加
					if (isexist) {
						insdpr.add(r);
					}
				} else {
					// 设备无投放记录时添加
					insdpr.add(r);
				}

			}
		}
		deviceProductRecordService.batchInsert(insdpr);
		deviceProductRecordService.batchUpdate(upddpr);
		return Result.successResult();

	}

	/**
	 * 获取正在投放总数
	 * @return
	 */
	@RequestMapping(value = "/getCount")
	@ResponseBody
	public Result getCount(HttpServletRequest request) {
		Long dprcount = null;
		//获取当前登录的用户
		UserDetails userDetails = (UserDetails) SecurityContextHolder.getContext().getAuthentication() .getPrincipal();
		userDetails.getUsername();
		if(userDetails.getUsername().equals("admin")){
			Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, SEARCH_PARAM_PREFIX);
			super.filterDataPermission(searchParams);
			dprcount= deviceProductRecordService.getCount(searchParams,null)+deviceAdsRecordService.getCount(searchParams, null);
		}else{
			//获取当前登录的用户详情
			User a= userService.findByLoginName(userDetails.getUsername());
			dprcount = deviceProductRecordService.getCount(null,a.getId())+deviceAdsRecordService.getCount(null, a.getId());
		}
		PagingVO paging = new PagingVO(new ArrayList<DeviceProductRecord>(),Integer.parseInt(String.valueOf(dprcount)), 0, 10);
		return Result.successResult(paging);

	}

}
